function main(defaultParams) {
    debugger;
    const self = this;
    const formPartEle = self.$$element("form_part_80r0ke");
    const formEle = formPartEle.$$getRenderElement();
    const pageRenderElement = this.$$root().$parent.$parent.$parent;

    let extraParams = self.state.extraParams;

    if (formPartEle) {
        formEle.$$validateData().then((res) => {
            if (!res.success) {
                var target = formEle.state.validate.target;
                return;
            } else {
                const formValue = formEle.$$getFormValue()['ds_tbl:t_355f38575f894287a8e40d8af41ae10c'];
                const unitTypeCode = formValue.checkbox_715py5 || []; // 多选机构类型

                const dataForSave = buildUnitInfo(formEle, formValue, unitTypeCode, extraParams);
                console.log("构建后的数据：", dataForSave);

                saveForm(dataForSave).then(res => {
                    console.log(res);
                    let hasError = JSON.parse(res.result).hasError
                    if (hasError) {
                        return;
                    }
                    pageRenderElement.goHistory(-1); // 保存成功后返回
                });
            }
        });
    }


}

function saveForm(data) {
    return api.DATA_SOURCE_EXECUTE(
        "ds_api:8222f7d2-792a-408d-9b80-2e6ed3160030",
        "api",
        [],
        [],
        JSON.stringify(data)
    );
}

function buildUnitInfo(formEle, formValue, unitTypeCode, extraParams) {
    const id = formValue && formValue.id;

    const unitBaseInfo = {
        id,
        unitName: formValue.text_input_6pb5tu,
        unitNameAbbr: formValue.text_input_b2uvyd,
        seniorId: formValue.tree_single_select_13q1ge,
        unitPropertyCode: formValue.single_select_gl6qfp,
        regionId: formValue.tree_single_select_1sohfs,
        regionName: formValue.tree_single_select_1sohfs_hidden_field_2uwjx2,
        usercenterUnitId: formValue.tree_single_select_bimaes,
        syncUsercenterFlag: formValue.radio_61g5vf,
        unitTypeCode: (unitTypeCode || []).join(","),
        socialCreditCode: formValue.text_input_5uxl5t,
        unitLayerLevelCode: formValue.single_select_aitj6e,
        legalDelegateMan: formValue.text_input_go3c3p,
        foundDate: formValue.date_picker_51oe0i,
        postalCode: formValue.text_input_agf1ki,
        email: formValue.text_input_4mdxvk,
        fax: formValue.text_input_2an17t,
        phone: formValue.text_input_bqewyt,
        address: formValue.text_input_aol9mx,
        unitIndustryTypeCode: formValue.single_select_197css,
        verticalManageStateCode: formValue.single_select_9l2zcc,
        verticalManageLevelCode: formValue.single_select_9fx606,
        verticalManageUnit: formValue.tree_single_select_85775k,
        unitStaffSize: formValue.number_input_86qfhb,
        settingBasis: formValue.textarea_dzpnbg,
        publicFlag: formValue.radio_ad7n9l
    };

    // 各种类型信息结构体
    const typeHandlers = {
        "1": {
            target: "superviseUnitInfoList", getData: () => ({
                id: extraParams.jdlx_id,
                unitPropertyCode: formValue.single_select_775354
            })
        },
        "2": {
            target: "superviseUnitInfoList", getData: () => ({
                id: extraParams.jdjg_id,
                unitPropertyCode: formValue.single_select_6h4dtp,
                superviseUnitId: formValue.tree_single_select_eyvn62
            })
        },
        "3": {
            target: "superviseUnitInfoList", getData: () => ({
                id: extraParams.jdlx_id,
                unitPropertyCode: formValue.single_select_775354
            })
        },
        "4": {
            target: "superviseUnitInfoList", getData: () => ({
                id: extraParams.jdjg_id,
                unitPropertyCode: formValue.single_select_6h4dtp,
                superviseUnitId: formValue.tree_single_select_eyvn62
            })
        },
        "5": {
            target: "subjectUnitInfoList", getData: () => ({
                id: extraParams.bmxz_id,
                unitPropertyCode: formValue.single_select_gl6qfp
            })
        },
        "6": {
            target: "subjectUnitInfoList", getData: () => ({
                id: extraParams.ztlb_id,
                subjectCategoryCode: formValue.single_select_4xhdme,
                lawSubjectId: formValue.tree_single_select_bw6dcb
            })
        },
        "7": {
            target: "subjectUnitInfoList", getData: () => ({
                id: extraParams.zfzt2_id,
                lawSubjectId: formValue.tree_single_select_a7pozd
            })
        },
        "8": {
            target: "delegationUnitInfoList", getData: () => {
                const table = formEle.$$getFormValue()["detail_table_hl9v69"] || [];
                return table
                    .filter(row => row.single_select_hoilag)
                    .map(row => ({
                        stdSubjectId: row.single_select_hoilag,
                        delegateModeType: row.single_select_f31blu,
                        settingBasis: row.text_input_mu7fk,
                        delegateStartDate: row.date_picker_51n8ah,
                        delegateEndDate: row.date_picker_9qltsk,
                        delegatePublicFlag: row.single_select_d7hy0z
                    }));
            }
        }
    };

    // 最终构建数据对象
    const result = {
        unitBaseInfo,
        superviseUnitInfoList: [],
        subjectUnitInfoList: [],
        delegationUnitInfoList: [],
        unitRelBizUnitInfoList: []
    };

    // 根据勾选的机构类型收集信息
    [...new Set(unitTypeCode)].forEach(code => {
        const handler = typeHandlers[code];
        if (handler) {
            const data = handler.getData();
            if (Array.isArray(data)) {
                result[handler.target].push(...data);
            } else if (data) {
                result[handler.target].push(data);
            }
        }
    });
    const unitTypeSet = new Set(unitTypeCode);
    const has13 = unitTypeSet.has("1") && unitTypeSet.has("3");
    const has24 = unitTypeSet.has("2") && unitTypeSet.has("4");

    const has5 = unitTypeCode.includes("5");
    const has6 = unitTypeCode.includes("6");
    const has7 = unitTypeCode.includes("7");


    if ((has13 || has24) && result.superviseUnitInfoList.length > 1) {
        const seenKeys = new Set();
        result.superviseUnitInfoList = result.superviseUnitInfoList.filter(item => {
            const key = JSON.stringify(item);
            if (seenKeys.has(key)) return false;
            seenKeys.add(key);
            return true;
        });
    }

    if (has5 || has6 || has7) {
        // 构造 unitRelBizUnitInfoList 数据（以当前表单已有字段来构建）
        result.unitRelBizUnitInfoList.push({
            id: extraParams.jdbm_id,
            relateTypeCode: "1",
            relateUnitId: formValue.tree_single_select_64bsmj,
            relateUnitName: ""
        });
        const relateUnitId = formValue.tree_single_select_6phfuk;

        if (relateUnitId) {
            result.unitRelBizUnitInfoList.push({
                id: extraParams.sjywbm_id,
                relateTypeCode: "2",
                relateUnitId,
                relateUnitName: ""
            });
        }
    }

    return result;
}
